<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
</head>

<body>
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-lg-offset3" layui-col-lg6>
                <div class="layui-form layuimini-form">
                    <form class="layui-form" action="" lay-filter="userForm">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <input type="hidden" name="id"/>
                            <div class="layui-input-block">
                                <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名"
                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <input type="hidden" name="id" value="" class="layui-input">
                        <div class="layui-form-item">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-block">
                                <input type="text" name="password" required lay-verify="required" placeholder="请输入密码"
                                    autocomplete="off" class="layui-input">
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">性别：</label>
                            <div class="layui-input-block">
                                <input type="radio" name="sex" value="男" title="男">
                                <input type="radio" name="sex" value="女" title="女" checked>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">年龄</label>
                            <div class="layui-input-block">
                                <input type="text" name="age" required lay-verify="required" placeholder="请输入年龄"
                                    autocomplete="off" class="layui-input">
                            </div>

                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">手机</label>
                            <div class="layui-input-block">
                                <input type="text" name="phone" required lay-verify="required" placeholder="请输入手机号"
                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                            </div>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>



    <script>
        //Demo
        layui.use('form', function () {
            var form = layui.form
                , $ = layui.jquery
                , laydate = layui.laydate;

            //日期
            laydate.render({
                elem: '#date'
            });


            // var data = JSON.parse(localStorage.getItem("user"));?    
            var id = localStorage.getItem("userId");
            console.log(id);
            
            $.post('http://localhost:8080/api/user/selectUserById',
                {
                    id: id
                },
                function (result) {
                    console.log(result);
                    if (result.code === 0) {
                        //给表单数据赋值
                        form.val('userForm', {
                            id:id,
                            username: result.data.name,
                            password: result.data.password,
                            sex: result.data.sex,
                            age: result.data.age,
                            phone: result.data.phone,

                        });
                    }


                }
            );






            //监听提交
            form.on('submit(formDemo)', function (data) {
                //layer.msg(JSON.stringify(data.field));
                console.log(data);

                // data.field.id = id;
                $.post(
                    'http://localhost:8080/api/user/update',
                   data.field,
                    function (result) {
                        if (result.code == 0) {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);

                        } else {
                            layer.msg(result.msg);
                        }
                    }
                );

            return false;
        });
        });
    </script>


</body>

</html>